package com.niuke;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * 52. 最佳的出牌方法
 * https://blog.csdn.net/qfc_128220/article/details/130775353
 */
public class NiukeMoni52 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String[] input = scanner.nextLine().split(" ");

        List<Integer> data = new ArrayList<>(input.length);
        for (int i = 0; i < input.length; i++) {
            if (input[i].equals("0")) {
                data.add(10);
            } else if (input[i].equals("J")) {
                data.add(11);
            } else if (input[i].equals("Q")) {
                data.add(12);
            } else if (input[i].equals("K")) {
                data.add(13);
            } else {
                data.add(Integer.parseInt(input[i]));
            }
        }

        // 逆序排列
        data.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer integer, Integer t1) {
                return t1 - integer;
            }
        });

        Map<Integer, Integer> counts = new HashMap<>();

        for (int i = 0; i < data.size(); i++) {
            int key = data.get(i);
            counts.put(key, counts.getOrDefault(key, 0) + 1);
        }

        int res = 0;
        for (Integer key : counts.keySet()) {
            int count = counts.get(key);
            if (count == 1) {
                res += key;
            } else if (count == 2) {
                res += key * 4;
            } else if (count == 3) {
                res += key * 6;
            } else if (count == 4) {
                res += key * 12;
            }
        }


        // 判断有顺子的情况，最多十五张牌，顶多三个顺子
        boolean hasShun = true;
        int tempRes = 0;
        while (data.size() > 0 && hasShun) {
            for (int i = 0; i < data.size(); i++) {
                int key = data.get(i);
                if (key > 9 || i < 4) {
                    if (i == data.size() - 1) {
                        hasShun = false;
                    }
                    continue;
                }

                if (counts.containsKey(key) && counts.containsKey(key + 1)
                        && counts.containsKey(key + 2) && counts.containsKey(key + 3)
                        && counts.containsKey(key + 4) && counts.get(key + 4) < 4 &&
                        counts.get(key + 3) < 4 && counts.get(key + 2) < 4) {
                    tempRes += (key + 2) * 10;
                    for (Integer j = key; j < key + 5; j++) {
                        int value = counts.get(j) - 1;
                        if (value <= 0) {
                            counts.remove(j);
                        } else {
                            counts.put(j, value);
                        }
                        data.remove(j);
                    }
                    break; // 断掉里面的循环，并且改变了data数据，继续执行外面的while循环
                }

                if (i == data.size() - 1) {
                    hasShun = false;
                }
            }
        }
        for (Integer key : counts.keySet()) {
            int count = counts.get(key);
            if (count == 1) {
                tempRes += key;
            } else if (count == 2) {
                tempRes += key * 4;
            } else if (count == 3) {
                tempRes += key * 6;
            } else if (count == 4) {
                tempRes += key * 12;
            }
        }

        System.out.println("res = " + res + ", tempRes = " + tempRes);
    }

}
